package org.wutopia.leo.resource;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.wutopia.leo.domain.payment.Stockpile;
import org.wutopia.leo.domain.payment.StockpileService;
import org.wutopia.leo.infrastructure.jaxrs.CodedMessage;
import org.wutopia.leo.infrastructure.jaxrs.CommonResponse;


/**
 * 库存相关的资源
 *
 **/

@RequestMapping("/api/v1/products")
@RestController
@CacheConfig(cacheNames = "resource.product")
public class StockpileResource {

    @Autowired
    private StockpileService service;

    /**
     * 将指定的产品库存调整为指定数额
     */

    @PatchMapping("/stockpile/{productId}")
    public ResponseEntity<CodedMessage> updateStockpile(@PathVariable("productId") Integer productId, @RequestParam("amount") Integer amount) {
        return CommonResponse.op(() -> service.set(productId, amount));
    }

    /**
     * 根据产品查询库存
     */

    @GetMapping("/stockpile/{productId}")
    public Stockpile queryStockpile(@PathVariable("productId") Integer productId) {
        return service.getByProductId(productId);
    }

}
